﻿using Ardalis.ApiEndpoints;
using Microsoft.AspNetCore.Mvc;
using TodoList.Data;

namespace TodoList.ArdalisEndpoints.Endpoints.TodoList;

public class Delete(ApplicationDbContext db) : EndpointBaseAsync
	.WithRequest<int>
	.WithActionResult
{
	[HttpDelete("api/[namespace]/{id}")]
	public override async Task<ActionResult> HandleAsync(int id, CancellationToken cancellationToken = default)
	{
		var item = await db.TodoItems.FindAsync(id, cancellationToken);
		if (item == null)
		{
			return NotFound("Todo item not found.");
		}
		db.TodoItems.Remove(item);
		await db.SaveChangesAsync(cancellationToken);
		return Ok("Todo item deleted successfully.");
	}
}
